package demo06;

        import org.dom4j.Document;
        import org.dom4j.DocumentException;
        import org.dom4j.Element;
        import org.dom4j.io.SAXReader;

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

public class test {
    public static void main(String[] args) throws DocumentException {
        queryList();

    }

    public static Map<String,Object> queryList() throws DocumentException {
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("rtnCode","0000");
        rtnMap.put("rtnMsg","查询成功");

        SAXReader sr = new SAXReader();
        Document doc = sr.read("areaInfo.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements();
        List<Map> proList = new ArrayList<>();
        for (Element e: elements) {
            Map<String,Object> proMap = new HashMap<>();
            proMap.put("pname",e.attributeValue("pname"));
            proMap.put("pid",e.attributeValue("pid"));
            List<Map> cityList = new ArrayList<>();
            List<Element> cityElements = e.elements();
            for (Element e1: cityElements) {
                Map<String,Object> cityMap = new HashMap<>();
                cityMap.put("cname",e1.attributeValue("cname"));
                cityMap.put("cid",e1.attributeValue("cid"));
                List<Map> countryList = new ArrayList<>();
                List<Element> countryElements = e1.elements();
                for (Element e2: countryElements) {
                    Map<String,Object> countryMap = new HashMap<>();
                    countryMap.put("cname",e2.attributeValue("tname"));
                    countryMap.put("cid",e2.attributeValue("tid"));
                    countryList.add(countryMap);
                }
                cityMap.put("countryList",countryList);
                cityList.add(cityMap);
            }
            proMap.put("cityList",cityList);
            proList.add(proMap);
        }
        rtnMap.put("proList",proList);

        return rtnMap;
    }
}
